package com.longma.dbgenerator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;
import java.util.Scanner;

public class DbGeneratorApplication {

	/**
	 * 读取控制台内容
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + "：");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "！");
	}

	/**
	 * 入口函数
	 * @param args
	 */
	public static void main(String[] args) {
		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		// 项目路径
		String projectPath = System.getProperty("user.dir") + "/db-generator";
		if (!new File(projectPath).isDirectory()){
			throw new MybatisPlusException("无该目录");
		}


		//输出目录，如果在一个模块内，需要修改代码 "moduleName/src/main/java"
		gc.setOutputDir(projectPath + "/src/main/generator");
		gc.setAuthor("HAIBO");
		gc.setOpen(false);
		gc.setSwagger2(true); //实体属性 Swagger2 注解
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/yuncang?useUnicode=true&useSSL=false&characterEncoding=utf8");
		// dsc.setSchemaName("public");
		dsc.setDriverName("com.mysql.jdbc.Driver");
		//dsc.setDbType(DbType.SQL_SERVER);//设置数据源，mysql不设置也能使用,SQLserver需要设置
		dsc.setUsername("root");
		dsc.setPassword("112233");
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		//子模块，可以注释掉
		pc.setModuleName("server");
		pc.setParent("com.longma.server");
		mpg.setPackageInfo(pc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		//strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		// 公共父类
		// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
		// 写于父类中的公共字段
		//strategy.setSuperEntityColumns("id");


		// CLGL_NJHTJ2_TB,CLGL_YJHTJ2_TB,CLGL_ZJHTJ2_TB,JCXX_BJB_TB,JCXX_BMB_TB,JCXX_CCJYB_TB,JCXX_CKWZB_TB,JCXX_CLB_TB,JCXX_CXB_TB,JCXX_CXZB_TB,JCXX_GYSB_TB,JCXX_QCB_TB,JCXX_RYB_TB,JCXX_WXXM_TB,JCXX_XNCSB_TB,JCXX_YHDLB_TB,JCXX_ZDB_TB,JHGL_BYJL_TB,JHGL_BYSCB_TB,JHGL_BYXMB_TB,JHGL_BYXMJDB_TB,JHGL_BYXMMXB_TB,JHGL_BYZY_TB,JHGL_BYZYB_TB,JHGL_CLLCDJB_TB,JHGL_GZZD_TB,JHGL_HJBY_TB,JHGL_JCJL_TB,JHGL_JHSS_BYZYB_TB,JHGL_PGJL_TB,JHGL_TSBY_TB,JHGL_XZZY_TB,JHGL_YCLLCDJB_TB,JHGL_ZCLLCDJB_TB,JHGL_ZJHXF_TB,QCGL_CCJHB_TB,QCGL_GJYQ_TB,QCGL_KCQD_TB,QCGL_PDB_TB,QCGL_QCCKB_TB,QCGL_QCRKB_TB,QCGL_QCXHB_TB,sysdiagrams,WXGL_JCJL_TB,WXGL_PGJL_TB,WXGL_WXBJ_TB,WXGL_WXGD_TB,WXGL_WXGJ_TB,WXGL_WXJL_TB,WXGL_WXJY_TB,WXGL_WXRY_TB,WXGL_WXSQ_TB,WXGL_WXXM_TB,WXGL_WXZY_TB,YWXX_DBJDJB_TB,YWXX_DBJSYB_TB,YWXX_WXJLB_TB,YWXX_ZBSYB_TB,ZG_TB
		strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
		//Controller驼峰命名方式
		strategy.setControllerMappingHyphenStyle(true);
		strategy.setTablePrefix(pc.getModuleName() + "t_");
		mpg.setStrategy(strategy);
		// mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}

}
